package com.mike;

import com.mike.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DubboConsumerMain {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        ExecutorService executorService = Executors.newCachedThreadPool();

        //编写一个消费端程序，不断调用Dubbo服务的3个方法
        while (true) {
            try {
                Thread.sleep(10);
                executorService.execute(() -> service.eat("fish", 100));
                executorService.execute(() -> service.sleep("the Arctic Ocean", 100));
                executorService.execute(() -> service.swim("the Arctic Ocean", 100));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.mike.bean")
    @EnableDubbo
    static class ConsumerConfiguration {
    }
}
